﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Tour.Linq.TourDBML;
public partial class AdminManager_Admin_AdminManager_Add : AdminBasePage
{
    protected int _UserID = 0;

    protected override void OnInit(EventArgs e)
    {
        _UserID = Request["AdminID"].ToInt();
        base.OnInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DisplayInfo();
        }
    }

    private void DisplayInfo()
    {
        if (_UserID > 0)
        {
            Admin admin = TourContext.Admin.First(m => m.ID == _UserID);
            txtUserName.Text = admin.UserName;
            txtUserName.Enabled = false;
        }
    }

    protected void lbtn_SaveUser(object sender, EventArgs e)
    {
        if (CheckEnterInfo())
        {
            _ReturnUrl = "Admin_AdminManager.aspx";
            Admin admin;
            if (_UserID > 0)
            {
                admin = TourContext.Admin.First(m => m.ID == _UserID);
                admin.Password = txtPassword.Text;
                _Message = "更新管理员成功";

            }
            else
            {
                admin = new Admin();
                admin.UserName = txtUserName.Text;
                admin.Password = txtPassword.Text;
                admin.AddTime = DateTime.Now;
                TourContext.Admin.InsertOnSubmit(admin);
                _Message = "添加新管理员成功";
            }
            TourContext.SubmitChanges();
        }
    }

    protected override bool CheckEnterInfo()
    {
        bool isCheck = false;
        if (txtUserName.Text.IsNullOrEmpty())
        {
            _Message = "用户名不能为空";
        }
        else if (txtPassword.Text.IsNullOrEmpty())
        {
            _Message = "密码不能为空";
        }
        else if (!txtPassword.Text.Equals(txtEnterPwd.Text))
        {
            _Message = "确认密码不一致";
        }
        else
        {
            isCheck = true;
        }
        return isCheck;
    }
}
